<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Index extends MY_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->model('Karas_model');
		$this->load->library('pagination');
		$this->load->library('session');
		if (! isset($_GET['test'])) {
            //redirect('/karaoke/home', 'refresh');
        }
        
        $this->view['fb_img'] = '';
        
        $this->load->model('Places_model');
	}
	
    public function index($city = 'ho-chi-minh') {
        $this->view['template'] = 'karaoke-places/index';
        $offset = $this->input->post('num_displayed');
            
        // Is ajax request
        if($this->input->is_ajax_request()) {
            // Find for current location
            $cur_lat = $this->input->post('lat');
            $cur_lng = $this->input->post('lng');
            $map_limit = $this->config->item('map_limit');
            $min_lat = $cur_lat - $map_limit;
            $max_lat = $cur_lat + $map_limit;
            $min_lng = $cur_lng - $map_limit;
            $max_lng = $cur_lng + $map_limit;
            
            $data = $this->Places_model->get_karaoke_places($min_lat, $max_lat, $min_lng, $max_lng);
            $current_location = false;
            if ($places = $data->result_array()) {
                $total  = count($places);
                $current_location = true;
            } else {
                $data   = $this->Places_model->get_limit_offset($this->per_page, $offset);
                $total  = $this->Places_model->count_all();
                $places = $data->result_array();
            }
            
            $view['data'] = array(
                'places'    => $places,
                'cur_lat'   => $cur_lat,
                'cur_lng'   => $cur_lng
            );	
            
            $html = $this->load->view('karaoke-places/_item',$view, TRUE);

            echo json_encode(array(
                'current_location'  => $current_location,
                'total'     => $total,
                'html'      => $html,
                'see_more'  => $total > $offset + $this->per_page,
                'places'    => $places, // For setting marker in the client
            ));
            exit;
        }
        
        $this->view['data'] = array (
            'places'        => array(),
            'see_more'      => true,
            'action'        => '',
            'home_js'       => 1,
            'load_map'      => true
        );
        
        $this->_render($this->view);		
    }
	
    public function filter_places($city = 'ho-chi-minh') {
        $offset = 15;
       
        $this->view['template'] = 'karaoke-places/index';
        $data   = $this->Places_model->get_offset_limit($this->per_page, $offset, array('provinceid' => $city));
        $total  = $this->Places_model->count_all(array('provinceid' => $city));
        
        if ($this->input->is_ajax_request()) {
            $view['data'] = array(
                'places'    => $data->result_array()
            );	
         
            $html = $this->load->view('karaoke-places/_item',$view, TRUE);

            echo json_encode(array(
                'total'     => $total,
                'html'      => $html,
                'see_more'  => $total > $offset + $this->per_page,
                'action'    => '/karaoke/'. $city
            ));
            exit;
        }
        
        $this->view['data'] = array (
			'places' 		=> $data->result_array(),
            'see_more'      => $total > $offset + $this->per_page,
            'active_city'   => $this->uri->segment(2),
            'action'        => '/karaoke/'. $city
		);
        
        $this->_render($this->view);
    }
    
    public function get_karaoke_places() {
        $cur_lat = $this->input->post('lat');
        $cur_lng = $this->input->post('lng');

        $map_limit = $this->config->item('map_limit');
        $min_lat = $cur_lat - $map_limit;
        $max_lat = $cur_lat + $map_limit;
        $min_lng = $cur_lng - $map_limit;
        $max_lng = $cur_lng + $map_limit;
        
        $offset  = $this->input->post('num_displayed');
        
        $this->load->model('Places_model');
        $data = $this->Places_model->get_karaoke_places($min_lat, $max_lat, $min_lng, $max_lng);
        
        $places = $data->result_array();
        $view['data'] = array(
            'places'    => array_slice($places, $offset, $this->per_page),  
            'cur_lat'   => $cur_lat,
            'cur_lng'   => $cur_lng
        );
        
        $html = $this->load->view('karaoke-places/_item',$view, TRUE);
        echo json_encode(array(
            'current_location' => true, 
            'total'     => count($places),
            'html'      => $html,
            'see_more'  => count($places) > $offset + $this->per_page,
            'places'    => $places // For setting marker in the client
        ));
        exit;
    }
    
    public function detail($id = null) {
        $this->view['template'] = 'karaoke-places/detail';
        $this->load->model('Places_model');
        $result = $this->Places_model->get_by_name($id);
        $result = $result->result_array();
        
        $this->view['title_page']       = $result[0]['name'];
        $this->view['fb_img']           = base_url(). 'public/static/karaoke/'.$result[0]['img'];
        $this->view['description_page'] = $result[0]['address'] . ' - '.$result[0]['phone'] ;
         
        $this->view['data']= array(
            'place'     => $result[0],
            'detail_js' => 1
        );
        
        $this->_render($this->view);
    }
}